package com.yjy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.comm.util.JsonUtils;
import com.system.common.pojo.SysMenu;
import com.system.common.pojo.SysRole;
import com.system.common.pojo.SysUser;
import com.yjy.service.SysRoleService;

@Controller
@RequestMapping("role")
public class SysRoleController {

	@Autowired
	private SysRoleService roleService;

	@RequestMapping("list")
	public String list(String rname, Model model, HttpSession session) {// 查询所有的角色信息
		Map<String, Object> map = new HashMap<>();
		if (rname != null) {
			map.put("rname", rname);
		}
		roleService.listAll(map);
		SysUser user = (SysUser) session.getAttribute("user");
		List<SysMenu> power = roleService.selectPower(user.getUserId());
		String menus = "";
		for (SysMenu menu : power) {
			menus += menu.getUrl() + ",";
		}
		model.addAttribute("power", menus);
		model.addAttribute("roleList", roleService.listAll(map));
		return "sys/sysrole";
	}

	@RequestMapping("delete")
	public String delete(String roleId) {// 删除角色
		roleService.delete(roleId);
		return "redirect:/role/list";
	}

	@RequestMapping(value = "checkName", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String checkName(String roleName) {// 角色名称是否重复
		boolean state = roleService.checkName(roleName);
		if (state) {
			return "ok";
		}
		return "error";
	}

	@RequestMapping("update")
	public String update(SysRole role) {// 修改角色信息
		roleService.update(role);
		return "redirect:/role/list";
	}

	@RequestMapping("save")
	public String save(SysRole role, HttpSession session) {// 添加新的角色
		SysUser user = (SysUser) session.getAttribute("user");
		role.setCreateUserId(user.getUserId());
		roleService.save(role);
		return "redirect:/role/list";
	}

	@RequestMapping(value = "clickZtree", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String clickZtree(String deptId) {// roleTree下的所有用户信息
		List<Map<String, Object>> users = roleService.clickZtree(deptId);
		String sysusers = JsonUtils.toJson(users);
		return sysusers;
	}

	@RequestMapping(value = "menuPower", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String menuPower(HttpServletRequest request) {// 根据roleId查询所有菜单信息
		String roleId = request.getParameter("roleId");
		List<SysMenu> menus = roleService.menuPower(roleId);
		String sysmenus = JsonUtils.toJson(menus);
		return sysmenus;
	}

	@RequestMapping(value = "selectnoUsers", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectnoUsers() {// 查询没有任何角色的用户信息
		List<Map<String, Object>> noUsers = roleService.nouserPower();
		String noUserss = JsonUtils.toJson(noUsers);
		System.out.println(noUserss);
		return noUserss;
	}

	@RequestMapping(value = "selectyesUsers", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectyesUsers(String roleId) {// 查询已经有某一个角色的用户信息
		List<Map<String, Object>> yesUsers = roleService.yesUserPower(roleId);
		String yesUserss = JsonUtils.toJson(yesUsers);
		System.out.println(yesUserss);
		return yesUserss;
	}

	@RequestMapping(value = "menutree", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String menuTree(HttpServletRequest request) {
		String roleId = request.getParameter("roleId");
		List<SysMenu> menus = roleService.menuPower(roleId);
		String sysmenus = JsonUtils.toJson(menus);
		return sysmenus;
	}

	@RequestMapping("PowerRole")
	public String PowerRole(String menus, String roleIds, HttpSession session) {
		roleService.PowerRole(menus, roleIds, session);
		return "redirect:/role/list";
	}

	@RequestMapping(value = "pick", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String pick(String ids, String checkusers, HttpSession session) {// 给用户赋予新的角色
		String info = roleService.pitchOn(ids, checkusers, session);
		return info;
	}

	@RequestMapping(value = "cancel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String cancel(String ids, String checkusers) {// 删除用户现有角色
		String info = roleService.cancel(ids, checkusers);
		return info;
	}
}
